package secret.santa.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SecretBox extends DialogBox implements ClickHandler, KeyUpHandler, SelectionHandler<Integer>
{
	
	private Button closeButton;
	private TabPanel eventPanel;
	private VerticalPanel inputPanel;
	private Button editWishList;
	private TextArea wishListTextBox;
	private Label statusLabel;
	private Label charsLeftLabel;
	
	private int index;
	
	private SecretData secret;
	
	Secret_Santa santa;
	SecretBox(Secret_Santa s)
	{
		santa = s;
		setText("Secret Santa");
		setAnimationEnabled(true);
		
		statusLabel = new Label();
		statusLabel.setStyleName("statusLabel");
		
		closeButton = new Button("Sluit dit venster");
		closeButton.getElement();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
		
		eventPanel = new TabPanel();
		eventPanel.addSelectionHandler(this);
		
		inputPanel = new VerticalPanel();
		
		wishListTextBox = new TextArea();
		wishListTextBox.setVisibleLines(10);
		wishListTextBox.addKeyUpHandler(this);
		wishListTextBox.setWidth("500px");
		inputPanel.add(new Label("Vul je lijstje hier in. (En druk daarna op de knop \"Verstuur je Lijstje\")"));
		
		HorizontalPanel h = new HorizontalPanel();
		charsLeftLabel = new Label();
		h.add(new Label("Aantal resterende karakters: "));
		h.add(charsLeftLabel);
		inputPanel.add(h);
		inputPanel.add(wishListTextBox);
		
		editWishList = new Button("Verstuur je lijstje");
		inputPanel.add(editWishList);
		editWishList.addClickHandler(this);
		
		dialogVPanel.add(eventPanel);
		dialogVPanel.add(inputPanel);
		dialogVPanel.add(statusLabel);
		dialogVPanel.add(closeButton);
		setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(this);
			
	}
	
	public void onClick(ClickEvent event) 
	{
		if(event.getSource()== closeButton)
		{
			hide();
			santa.logout();
		}
		else if(event.getSource()== editWishList)
			editWishList();
	}

	private void editWishList() 
	{
		String wishList = wishListTextBox.getText();
		if(wishList.length() > 500)
			Window.alert("Er is een limiet van 500 karakters voor je lijstje.\nMaak je lijstje korter om het te kunnen verzenden.");
		else
			santa.editWishList(secret.eventEntryKeys[index], wishList);
	}
	public void displayData(SecretData d)
	{
		secret = d;
		
		int size = d.eventEntryKeys.length;
		
		
		VerticalPanel[] dataPanel = new VerticalPanel[size];
		FlexTable[] data = new FlexTable[size];
		
		
		eventPanel.clear();
		
		
		for(int i = 0; i < size; i++)
		{
			dataPanel[i] = new VerticalPanel();
			data[i] = new FlexTable();
			eventPanel.add(dataPanel[i], d.event[i]);
			dataPanel[i].add(data[i]);
			data[i].setBorderWidth(2);
			data[i].setWidget(0, 0, new Label("Hallo, " + d.name));
			data[i].setWidget(1, 0, new Label("Jij komt naar: " + d.event[i]));
			//data[i].setWidget(2, 0, new Label(d.eventDescriptions[i]));
			data[i].setWidget(3,0, new Label("Jij moet een cadeau kopen voor: " + d.names[i]));
			data[i].setWidget(4,0, new Label(d.names[i] + "'s lijstje:"));
			TextArea wishListBox = new TextArea();
			wishListBox.setEnabled(false);
			wishListBox.setVisibleLines(10);
			wishListBox.setWidth("500px");
			wishListBox.setText(d.santaWishlist[i]);
			data[i].setWidget(5,0, wishListBox);
			
			//dataPanel[i].add(inputPanel);
			//wishListTextBox.setText(d.yourWishlist[i]);	
		}
		eventPanel.selectTab(0);
		show();
	}

	@Override
	public void onSelection(SelectionEvent<Integer> event) 
	{
		index = event.getSelectedItem().intValue();
		
		wishListTextBox.setText(secret.yourWishlist[index]);
		
		int length = wishListTextBox.getText().length();
		
		charsLeftLabel.setText(String.valueOf(500 - length));
	}

	public void setStatus(String string)
	{
		statusLabel.setText(string);
		
	}

	@Override
	public void onKeyUp(KeyUpEvent event) 
	{
		
		int length = wishListTextBox.getText().length();
		
		charsLeftLabel.setText(String.valueOf(500 - length));
		
	}

}